package com.qidian.permission.service.Impl;

import com.qidian.permission.entity.Role;
import com.qidian.permission.mapper.RoleMapper;
import com.qidian.permission.service.RoleService;
import com.qidian.permission.util.entity.Pager;
import com.qidian.permission.util.entity.PerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author 18林梦雅
 * @Company 起点编程
 * @Time 2023/10/21 1:28
 * @Description
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PerResult queryByPage(Role role, int page, int limit) {
        System.out.println(role +"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        Pager<Role> pager=new Pager<>();
        pager.setCurr(page);
        pager.setLimit(limit);
        pager.setTotal( roleMapper.selectCount(role));
        pager.setData(roleMapper.selectByPage(role,(page-1)*limit,limit));
        pager.setMaxPageNum((int) Math.ceil(pager.getTotal()*1.0/limit));
        return PerResult.ok(pager);
    }

    @Override
    public PerResult addRole(Role role) {
        //查找是否重名
        if(Objects.isNull(roleMapper.selectByName(role.getRoleName()))){
            //为空则不重名
            return PerResult.ok(roleMapper.addRole(role)+" ");
        }
        return PerResult.duplicate("用户名重复!");
    }

    @Override
    public PerResult selectByRoleId(Long roleId) {
        return PerResult.ok(roleMapper.selectByRoleId(roleId));
    }


    @Override
    public PerResult update(Role role) {
        Role roleTemp=roleMapper.selectByName(role.getRoleName());
        if(roleTemp!=null&&!roleTemp.getRoleId().equals(role.getRoleId())){
            return PerResult.duplicate("用户名重复");
        }
        return PerResult.updateIsOk(roleMapper.update(role));
    }

    @Override
    public PerResult deleteUpdateStatus(Long roleId) {
        return PerResult.ok(roleMapper.deleteUpdateStatus(roleId));
    }



    @Override
    public PerResult deleteUpdateSelect(Long[] roleIds) {
        int count=roleMapper.deleteUpdateSelect(roleIds);
        return PerResult.ok(count+" ");
    }
}
